package com.yanqu.road.dao.impl.activity.summergames;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.summergames.SummerGamesExchangeTip;
import com.yanqu.road.entity.activity.summergames.SummerGamesLottery;
import com.yanqu.road.entity.activity.summergames.SummerGamesSelectReward;
import com.yanqu.road.entity.activity.summergames.SummerGamesUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SummerGamesUserDataDaoImpl extends TempDataObjectDao<SummerGamesUserData> {

    public static String getSummerGamesUserDataSql = "select * from `t_u_summer_games_user_data` where `activityId`=? and `userId`=?";
    public SummerGamesUserData getSummerGamesUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSummerGamesUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateSummerGamesUserDataSql = "update `t_u_summer_games_user_data` set score=?,guessScore=?,poolDrawProcess=?,drawTimes=?,dayGetGuessItemMap=?,medalsMap=?,convertNumMap=?,exchangeTipMap=?,convertSelectMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(SummerGamesUserData summerGamesUserData) {
        summerGamesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getGuessScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getPoolDrawProcess())));
        para.put(i++, new DbParameter(Types.INTEGER, summerGamesUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getDayGetGuessItemMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getMedalsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getConvertNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getExchangeTipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getConvertSelectMap())));
        para.put(i++, new DbParameter(Types.INTEGER, summerGamesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSummerGamesUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSummerGamesUserDataSql = "insert into `t_u_summer_games_user_data`(activityId,userId,score,guessScore,poolDrawProcess,drawTimes,dayGetGuessItemMap,medalsMap,convertNumMap,exchangeTipMap,convertSelectMap) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SummerGamesUserData summerGamesUserData) {
        summerGamesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, summerGamesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, summerGamesUserData.getGuessScore()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getPoolDrawProcess())));
        para.put(i++, new DbParameter(Types.INTEGER, summerGamesUserData.getDrawTimes()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getDayGetGuessItemMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getMedalsMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getConvertNumMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getExchangeTipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(summerGamesUserData.getConvertSelectMap())));
        if (getDb().executeNoneQuery(insertSummerGamesUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SummerGamesUserData parseData(ResultSet rs) throws SQLException {
        SummerGamesUserData summerGamesUserData = new SummerGamesUserData();
        summerGamesUserData.setActivityId(rs.getInt("activityId"));
        summerGamesUserData.setUserId(rs.getLong("userId"));
        summerGamesUserData.setScore(rs.getLong("score"));
        summerGamesUserData.setGuessScore(rs.getLong("guessScore"));
        summerGamesUserData.setPoolDrawProcess(JSON.parseObject(rs.getString("poolDrawProcess"), new TypeReference<Map<Integer, SummerGamesLottery>>() {}));
        summerGamesUserData.setDrawTimes(rs.getInt("drawTimes"));
        summerGamesUserData.setDayGetGuessItemMap(JSON.parseObject(rs.getString("dayGetGuessItemMap"), new TypeReference<Map<Long, Integer>>() {}));
        summerGamesUserData.setMedalsMap(JSON.parseObject(rs.getString("medalsMap"), new TypeReference<Map<Integer, Integer>>() {}));
        summerGamesUserData.setConvertNumMap(JSON.parseObject(rs.getString("convertNumMap"), new TypeReference<Map<Integer, Integer>>() {}));
        String exchangeTipMap = rs.getString("exchangeTipMap");
        if(!StringUtils.isNullOrEmpty(exchangeTipMap)) {
            summerGamesUserData.setExchangeTipMap(JSON.parseObject(exchangeTipMap, new TypeReference<ConcurrentHashMap<Long, SummerGamesExchangeTip>>() {
            }));
        }
        String convertSelectMap = rs.getString("convertSelectMap");
        if(!StringUtils.isNullOrEmpty(convertSelectMap)) {
            summerGamesUserData.setConvertSelectMap(JSON.parseObject(convertSelectMap, new TypeReference<ConcurrentHashMap<Integer, SummerGamesSelectReward>>() {
            }));
        }
        return summerGamesUserData;
    }
}
